﻿<%@ WebHandler Language="C#" Class="ExecuteHandler"%>

using System;
using System.Web;
using System.Data;
using System.Globalization;
public class ExecuteHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        //context.Response.ContentType = "text/plain";
        //1. 传值方式为GET时：context.Request.Params.Get("id")
        //2. 传值方式为POST时：Request.Params["id"].toString()
        //context.Response.ContentType = "application/json";      
        context.Response.ContentType = "application/json";
        string sql = null,
                timestamp = null,
                filename = null,
                table = null,
                id = null,
                imagelist = null,
                title = null,
                classname = null,
                content = null,
                Flags = null,
                mold=null,
                time = null;
       
        string type = context.Request.Params["type"].ToString(); //POST提交的数据

        //var data = context.Request;
        //var sr = new StreamReader(data.InputStream);
        //var stream = sr.ReadToEnd();
        //var javaScriptSerializer = new JavaScriptSerializer();
        DB excute = new DB();
        switch (type)
        {
            case "insert":
                id = context.Request.Params["classid"];
                imagelist = context.Request.Params["imagelist"];
                title = context.Request.Params["title"];
                classname = context.Request.Params["classname"];
                content = context.Request.Params["content"];
                time = context.Request.Params["t"];
                timestamp = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo);
                Flags= context.Request.Params["Flags"];
                filename = Flags+"_"+ timestamp;
                table = context.Request.Params["table"];
                
                mold=context.Request.Params["mold"];
                DataTable dt = new DataTable();
                dt.Columns.Add("title");
                dt.Columns.Add("classname");
                dt.Columns.Add("content");
                dt.Columns.Add("time");
                dt.Rows.Add(title, classname, content,time);              
                sql = "insert into "+table+" values('" + id + "','" + imagelist + "','" + classname + "','" + title + "','" + content + "','" + time + "','" + filename + "')";
                create(dt, filename,Flags,mold);
                break;
            case "update":
                id = context.Request.Params["id"];
                imagelist = context.Request.Params["imagelist"];
                title = context.Request.Params["title"];
                classname = context.Request.Params["classname"];
                content = context.Request.Params["content"];
                time = context.Request.Params["time"];
                Flags= context.Request.Params["Flags"];
                mold=context.Request.Params["mold"];
                filename = context.Request.Params["filename"];
                table= context.Request.Params["table"];
                dt = new DataTable();
                dt.Columns.Add("title");
                dt.Columns.Add("content");
                dt.Columns.Add("classname");
                dt.Columns.Add("imagelist");
                dt.Columns.Add("time");
                dt.Rows.Add(title, content, classname, imagelist, time);
                sql = "UPDATE " + table + " set " +  "imagelist='" + imagelist + "',classname='" + classname + "',title='" + title + "',content='" + content + "',time='" + time + "'" + " WHERE id = '" + id + "'";
                create(dt, filename,Flags,mold);
                break;
            case "delete":
                break;
            default: break;
        }



        if (excute.Execte(sql))
        {
            context.Response.Write("成功");
            context.Response.End();
        }
        else { context.Response.Write("失败"); context.Response.End(); }

    }
    private void create(DataTable dt,string filename, string Flags,string mold)
    {
        switch (Flags)
        {
            case "news":
                using (StaticBase sb = new OutPage(dt, FlagsFileName.News, filename, mold))
                {
                    if (!sb.Osucess)
                    {

                        System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('" + sb.Errorstring + "')</script>");
                    }
                }
                break;
            case "products":
                using (StaticBase sb = new OutPage(dt, FlagsFileName.Products, filename, mold))
                {
                    if (!sb.Osucess)
                    {

                        System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('" + sb.Errorstring + "')</script>");
                    }
                }
                break;
            default: break;
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}